<?php

namespace App\Http\Controllers\Index;

use App\Models\Cate;
use App\Models\Member;
use App\Models\Article;
use App\Models\Comment;
use Illuminate\Http\Request;
use App\Http\Controllers\IndexController;

class Index extends IndexController
{
    //前端首页
    public function index()
    {
        $where = [];
        $catename = null;
        if (request('id')) {
            $where = [
                'cate_id' => request('id')
            ];
            $catename = Cate::where('id', request('id'))->value('catename');
        }
        $articles = Article::orderBy('created_at', 'desc')->where('status', 1)->where($where)->paginate(5);
        $viewData = [
            'articles' => $articles,
            'catename' => $catename
        ];
        return view('index.index.index', $viewData);
    }

    //文章详情页
    public function info()
    {
        $articleInfo = Article::find(request('id'));
        $viewData = [
            'articleInfo' => $articleInfo
        ];
        return view('index.index.info', $viewData);
    }

    //注册
    public function register()
    {
        if (request()->isMethod('post')) {
            $data = request()->only(['username', 'password', 'conpass', 'nickname', 'email', 'verify']);
            $result = (new Member)->register($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg' => '注册成功',
                    'url' => url('login')
                ];
            }else {
                $msg = [
                    'code' => 0,
                    'msg' => $result
                ];
            }
            return $msg;
        }
        return view('index.index.register');
    }

    //登录
    public function login()
    {
        if (request()->isMethod('post')) {
            $data = request()->only(['username', 'password', 'verify']);
            $result = (new Member)->login($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg' => '登录成功'
                ];
            }else {
                $msg = [
                    'code' => 0,
                    'msg' => $result
                ];
            }
            return $msg;
        }
        return view('index.index.login');
    }

    //退出
    public function loginout()
    {
        session()->flush();
        $msg = [
            'code' => 1,
            'msg' => '退出成功'
        ];
        return $msg;
    }

    //评论
    public function comm()
    {
        $data = request()->only(['article_id', 'member_id', 'content']);
        $result = (new Comment)->add($data);
        if ($result == 1) {
            $msg = [
                'code' => 1,
                'msg' => '评论成功'
            ];
        }else {
            $msg = [
                'code' => 0,
                'msg' => $result
            ];
        }
        return $msg;
    }

    //搜索功能
    public function search()
    {
        $catename = request('search');
        $articles = Article::orderBy('created_at', 'desc')->where('status', 1)->where('title', 'like', '%' . request('search') . '%')->paginate(5);
        $viewData = [
            'articles' => $articles,
            'catename' => $catename
        ];
        return view('index.index.index', $viewData);
    }
}
